package com.heima.reggie.controller;

import com.aliyun.dysmsapi20170525.Client;
import com.aliyun.dysmsapi20170525.models.SendSmsRequest;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.heima.reggie.common.R;
import com.heima.reggie.common.Sample;
import com.heima.reggie.common.ValidateCodeUtils;
import com.heima.reggie.pojo.User;
import com.heima.reggie.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

@RestController
@RequestMapping("/user")
@Slf4j
public class UserController {
    @Autowired
    private UserService userService;
@PostMapping("/sendMsg")
    public R<String> sendMsg(@RequestBody User user, HttpServletRequest request) throws Exception {
    String phone = user.getPhone();

    if (phone != null) {
      //  Integer integer = ValidateCodeUtils.generateValidateCode(4);
        Integer integer = new Integer(1234);
        String s = integer.toString();
        /**
         * 验证码转义为阿里云要求格式
         *   String yanzhengma=new String("{code:"+s+"}");
         *         String yanzhengma2=new String("{\"code\":\""+s+"\"}");
         *
         *         com.aliyun.dysmsapi20170525.Client client = Sample.createClient("LTAI5tHJuTwHo1sd4uoLuocC", "PDHp9hNmX7OSnEunPyau76xvy5Bf41");
         *
         *         SendSmsRequest sendSmsRequest = new SendSmsRequest()
         *                 .setSignName("阿里云短信测试")
         *                 .setTemplateCode("SMS_154950909")
         *                 .setPhoneNumbers(phone)
         *                 .setTemplateParam(yanzhengma2);
         *         // 复制代码运行请自行打印 API 的返回值
         *         client.sendSms(sendSmsRequest);
         */


  request.getSession().setAttribute(phone,s);


        return R.success("验证码已发送至"+phone+s);

    }
    return  R.error("验证码发送失败");
};
@PostMapping("/login")
public R<String> login(@RequestBody Map map , HttpServletRequest request){
    String phone = (String) map.get("phone");
    String code = (String) map.get("code");
    String attribute = (String) request.getSession().getAttribute(phone);
    LambdaQueryWrapper<User> lamd = new LambdaQueryWrapper<User>();
    lamd.eq(phone!=null,User::getPhone,phone);

    if (attribute !=null && attribute.equals(code)) {
        int count = userService.count(lamd);
        if (count<1) {
            User user = new User();
            user.setPhone(phone);
            userService.save(user);
            Long id = user.getId();
            log.info("id--------->{}",id);
request.getSession().setAttribute("user",id);

        }

        Long id = userService.getOne(lamd).getId();

        request.getSession().setAttribute("user",id);
        return R.success("验证码正确,登陆成功");



    }




    return R.error("验证码错误");
}






}
